package com.binhthuan.catechist.client.data;

import com.binhthuan.catechist.shared.constant.Parameter;
import com.binhthuan.catechist.shared.constant.Service;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.widgets.form.fields.SelectItem;

public class GradeDS extends AbstractDS {

    private static GradeDS instance;

    private GradeDS() {
        super(Service.GRADE);

        DataSourceIntegerField id = new DataSourceIntegerField(Parameter.ID);
        id.setHidden(true);
        id.setPrimaryKey(true);

        DataSourceTextField name = new DataSourceTextField(Parameter.NAME);
        name.setRequired(true);
        
        SelectItem leaderEditor = new SelectItem(Parameter.LEADER);
        leaderEditor.setValueField(Parameter.ID);
        leaderEditor.setDisplayField(Parameter.FULL_NAME);
        leaderEditor.setOptionDataSource(TrainerDS.getInstance());
        //leaderEditor.setDefaultToFirstOption(true);

        DataSourceTextField leader = new DataSourceTextField(Parameter.LEADER);
        leader.setValueXPath(combineXPath(Parameter.LEADER, Parameter.ID));
        leader.setTypeAsDataSource(TrainerDS.getInstance());
        leader.setEditorType(leaderEditor);

        setFields(id, name, leader);
    }

    public static GradeDS getInstance() {
        if (instance == null) {
            instance = new GradeDS();
        }
        return instance;
    }
}
